package com.sq.goodline.util.http;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration 
public class HttpClientConfig {
	@Value("${http.pool.maxTotal}")
	private Integer maxTotal;

	@Value("${http.pool.defaultMaxPerRoute}")
	private Integer defaultMaxPerRoute;

	@Value("${http.request.connectTimeout}")
	private Integer connectTimeout;

	@Value("${http.request.connectionRequestTimeout}")
	private Integer connectionRequestTimeout;

	@Value("${http.request.socketTimeout}")
	private Integer socketTimeout;

	@Bean(name = "httpClientConnectionManager")
	public PoolingHttpClientConnectionManager getHttpClientConnectionManager() {
		PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
		httpClientConnectionManager.setMaxTotal(maxTotal);
		httpClientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
		httpClientConnectionManager.setValidateAfterInactivity(2000);
		return httpClientConnectionManager;
	}

	@Bean(name = "httpClientBuilder")
	public HttpClientBuilder getHttpClientBuilder(
			@Qualifier("httpClientConnectionManager") PoolingHttpClientConnectionManager httpClientConnectionManager) {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		httpClientBuilder.setConnectionManager(httpClientConnectionManager);
		return httpClientBuilder;
	}
	
	@Bean
	public CloseableHttpClient getCloseableHttpClient(@Qualifier("httpClientBuilder")HttpClientBuilder httpClientBuilder){ 
		return httpClientBuilder.build(); 
	}
	
	@Bean(name = "builder")
	public RequestConfig.Builder getBuilder(){
	    RequestConfig.Builder builder = RequestConfig.custom();
	    return builder.setConnectTimeout(connectTimeout)
	            .setConnectionRequestTimeout(connectionRequestTimeout)
	            .setSocketTimeout(socketTimeout);
	}

	@Bean
	public RequestConfig getRequestConfig(@Qualifier("builder") RequestConfig.Builder builder){
	    return builder.build();
	}   
	
}
